package com.honey.compilation.sql.ddl;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlComment;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.TimeUtility;

/**
 * SQL 92 的注释
 * @author Administrator
 *
 */
public class SQL92Comment implements SqlComment{
	
	private List<String> comments;
	
	/**
	 * 注释分隔符,有效的ASCII字符范围是32～126之间
	 */
	private char splitCharacter;

	/**
	 * 注释分割符长度
	 */
	private int splitCharacterLength;
	
	public SQL92Comment (){
		comments = new ArrayList<String>();
		splitCharacter = 0 ;
		splitCharacterLength =30;
	}
	
	
	/**
	 * 内置注释字段 <br />
	 * @Author 添加作者(获取当前登录系统用户)
	 * @Date 添加时间(或其当前时间)
	 * @Version 添加版本号
	 * @Os 操作系统名称
	 * @Description 添加描述
	 */
	@Override
	public void addComments(String ...comments){
		for( String comment : comments){
			if(comment != null){
				this.comments.add(comment);
			}
		}
	}

	/**
	 * 注释分割符号
	 * @return
	 */
	public char getSplitCharacter() {
		return splitCharacter;
	}

	/**
	 * 注释分割符号,有效的ASCII字符范围是32～126之间(可视字符范围)
	 * @param splitCharacter
	 */
	public void setSplitCharacter(char splitCharacter) {
		if(splitCharacter >= 32 && splitCharacter <= 126 ){
			this.splitCharacter = splitCharacter;
		}
	}
	
	
	/**
	 * 注释分隔符号长度,默认30,最小值0 最大值100 ,超出范围使用默认值.
	 * @return
	 */
	public int getSplitCharacterLength() {
		return splitCharacterLength;
	}
	
	
	/**
	 * 注释分隔符号长度,默认30,最小值0 最大值100 ,超出范围使用默认值.
	 * @param splitCharacterLength 
	 */
	public void setSplitCharacterLength(int splitCharacterLength) {
		if(splitCharacterLength>=0 && splitCharacterLength<=100 ){
			this.splitCharacterLength = splitCharacterLength;
		}
	}
	
	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC256StringBuilder();
		String split = getFormatsplitCharacter();
		if( split != null ){
			IndentSpace.spaceIndent(answer, indentLevel);
			answer.append(split);
		}
		for(String comment : comments){
			comment = getFormatAuthor(comment);
			comment = getFormatDate(comment) ;
			comment = getFormatVersion(comment);
			comment = getFormatOsname(comment);
			comment = getFormatDescription(comment);
			comment = getFormatComment(comment);
			IndentSpace.spaceIndent(answer, indentLevel);
			answer.append( SqlKeyWord.OPERATION_COMMENT ).append( comment );
			IndentSpace.newLine(answer);
		}
		if( split != null ){
			IndentSpace.spaceIndent(answer, indentLevel);
			answer.append(split);
		}
		
		return answer;
	}
	private String getFormatComment(String comment){
		String answer= null;
		if(splitCharacter >= 32 && splitCharacter <= 126 && comments.size()>0 ){
			int commentLength=comment.length();
			if(commentLength < splitCharacterLength){
				CharacterBuilder sb = CharacterBuilderFactory.createC16StringBuilder();
				sb.append(comment);
				for( int i =0,size =splitCharacterLength - commentLength ;i < size ;i++){
					sb.append(SqlKeyWord.OPERATION_SPACE);
				}
				sb.append(SqlKeyWord.OPERATION_SPACE).append(SqlKeyWord.OPERATION_COMMENT.getValue());
				answer=sb.toString();
			}else{
				answer = comment;
			}
		}else{
			answer = comment;
		}
		return answer ;
	}
	
	private String getFormatsplitCharacter(){
		String answer= null;
		if(splitCharacter >= 32 && splitCharacter <= 126 && comments.size()>0 ){
			CharacterBuilder sb = CharacterBuilderFactory.createC128StringBuilder();
			sb.append(SqlKeyWord.OPERATION_COMMENT);
			for( int i =0;i < splitCharacterLength ;i++){
				sb.append(splitCharacter);
			}
			sb.append(SqlKeyWord.OPERATION_SPACE).append(SqlKeyWord.OPERATION_COMMENT.getValue());
			IndentSpace.newLine(sb);
			answer=sb.toString();
		}
		return answer ;
	}	
	
	/**
	 * 
	 * @param author
	 * @return
	 */
	private String getFormatAuthor(String author ){
		String answer= null;
		if( author.startsWith("@Author")){
			answer ="     Author : ";
			if(author.length() == 7){
				answer += System.getProperty("user.name");
			}else{
				answer +=author.substring(7, author.length());
			}
		}else{
			answer = author;
		}
		return answer ;
	}
	
	/**
	 * 
	 * @param date 
	 * @return
	 */
	private String getFormatDate(String date ){
		String answer= null;
		if( date.startsWith("@Date")){
			answer ="Create Date : " +TimeUtility.format(TimeUtility.FORMAT_DATE_ONLY);
		}else{
			answer = date;
		}
		return answer ;
	}
	
	private String getFormatVersion(String version ){
		String answer= null;
		if( version.startsWith("@Version")){
			answer ="    Version : SQL 92" ;
		}else{
			answer = version;
		}
		return answer ;
	}
	
	private String getFormatDescription(String desc ){
		String answer= null;
		if( desc.startsWith("@Description")){
			answer ="Description : ";
			if(desc.length() >12){
				String s =desc.substring(12, desc.length()); 
				answer += s.trim();
			}
		}else{
			answer = desc;
		}
		return answer ;
	}

	
	private String getFormatOsname(String osName ){
		String answer= null;
		if( osName.startsWith("@Os")){
			answer ="         OS : "+System.getProperty("os.name");;
		}else{
			answer = osName;
		}
		return answer ;
	}


	@Override
	public List<String> getComments() {
		return comments;
		
	}
	
	
	
}
